﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Final_exam
{
    public partial class Editerform : Form
    {
        public Editerform()
        {
            InitializeComponent();
        }

        private int Id;

        public Editerform(int Id,string name,int age,int sco)
        {
            InitializeComponent();
            this.Id = Id;
            textBox1.Text = name;
            textBox2.Text = age.ToString();
            textBox3.Text = sco.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var age = textBox2.Text;
            var sco = textBox3.Text;
            
            if (this.Id > 0)
            {
                var sql = string.Format("update Students set Name='{0}',Age='{1}',Score={2} where Id={3} ", name, age, sco,this.Id);
                DbHepler.AddUpdateordelete(sql);
                MessageBox.Show("恭喜您，更新成功!");
            }
            else
            {
                var sql = string.Format("insert into Students values ('{0}',{1},{2})", name, age, sco);
                DbHepler.AddUpdateordelete(sql);
                MessageBox.Show("恭喜您，添加成功!");
            }

            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
